package com.web.service.impl;

import com.dto.BaseDto;
import com.dto.MessageEnum;
import com.entity.Condition;
import com.entity.Dietitian;
import com.exception.CustomException;
import com.page.PageInfo;
import com.web.dao.DietitianDao;
import com.web.dao.RedisGeneratorDao;
import com.web.service.DietitianService;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;
import java.util.Optional;

@Service
public class DietitianServiceImpl implements DietitianService{
    @Resource
    DietitianDao dietitianDao;
    @Resource
    RedisGeneratorDao redisGeneratorDao;
//
//    public BaseDto<Dietitian> getAll() throws Exception {
//        List<Dietitian> dietitians = dietitianDao.getAll();
//        MessageEnum messageEnum = dietitians.size() > 0? MessageEnum.GETALLSTUDENTSUCCESS:MessageEnum.GETALLSTUDENTNODATA;
//        BaseDto<Dietitian> dto = new BaseDto(messageEnum,dietitians);
//
//        return dto;
//    }





    //表示首先到 缓存管理器  myCache 找有没有数据，根据cacheId
    @Cacheable(value = "myCache",key = "#cacheId")
    public BaseDto<Dietitian> getDietitiansByPage(PageInfo pageInfo,String cacheId) throws Exception {
        List<Dietitian> dietitians = dietitianDao.getDietitiansByPage(pageInfo);
        MessageEnum messageEnum = dietitians.size() > 0? MessageEnum.GETCUSTOMERBYPAGESUCCESS:MessageEnum.GETCUSTOMERBYPAGEFAIL;
        BaseDto<Dietitian> dto = new BaseDto(messageEnum,dietitians);

        return dto;
    }



    public BaseDto<String> searchDietitianIdsByName(String petternName) throws Exception {
        List<String> dietitianIds = dietitianDao.searchDietitianIdsByName(petternName);
        MessageEnum messageEnum = dietitianIds.size() > 0? MessageEnum.GETCUSTOMERBYPAGESUCCESS:MessageEnum.GETCUSTOMERBYPAGEFAIL;
        BaseDto<String> dto = new BaseDto(messageEnum,dietitianIds);

        return dto;
    }

    //表示首先到 缓存管理器  myCache 找有没有数据，根据cacheId
    public BaseDto<Dietitian> getDietitiansByPage(PageInfo pageInfo) throws Exception {
        List<Dietitian> dietitians = dietitianDao.getDietitiansByPage(pageInfo);
        MessageEnum messageEnum = dietitians.size() > 0? MessageEnum.GETCUSTOMERBYPAGESUCCESS:MessageEnum.GETCUSTOMERBYPAGEFAIL;
        BaseDto<Dietitian> dto = new BaseDto(messageEnum,dietitians);

        return dto;
    }

    /**
     * 添加会员
     * @param dietitian
     * @return
     * @throws Exception
     */
    public BaseDto<Dietitian> add(Dietitian dietitian) throws Exception {
        int result = dietitianDao.add(dietitian);
        MessageEnum messageEnum = result > 0? MessageEnum.ADDCUSTOMERSUCCESS:MessageEnum.ADDCUSTOMERFAIL;
        BaseDto<Dietitian> dto = new BaseDto(messageEnum,result);

        return dto;
    }




}
